package dynamicprogramming;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/2 22:05
 * https://leetcode-cn.com/problems/unique-binary-search-trees/
 */
public class NumTrees {

    /**
     * dp[i]表示结点数目为i的二叉搜索树不同的树类型
     * 递推公式：dp[i] += dp[j-1]*dp[i-j]   j{1....i}
     * 初始化：dp[0] = 1 dp[1] = 1 
     * @param n
     * @return
     */
    public int numTrees(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }
        return dp[n];
    }

    public static void main(String[] args) {

    }
}
